<?php

namespace app\index\controller;

use think\Controller;
use think\Session;
use think\Request;
use think\View;
use think\Db;
use app\index\validate\Valid;
use PHPExcel;
use PHPExcel_Writer_Excel5;
use PHPExcel_Style_Alignment;
use PHPExcel_Style_Fill;
use PHPExcel_Style_Border;
use PHPExcel_Cell_DataType;

class Commonwork extends Common
{
	public $typearr=[
                        5=>["name"=>"出差申请","table"=>"chuchai_apply"],
                        6=>["name"=>"万用申请","table"=>"wanneng_apply"],
                        7=>["name"=>"经销商合同申请","table"=>"jxs_hetong"],
                        8=>["name"=>"预算及目标申请","table"=>"target_apply"],
                        9=>["name"=>"预算及目标异动申请","table"=>"target_apply"],
                        10=>["name"=>"日常费用报销申请","table"=>"rc_baoxiao_apply"],
                        11=>["name"=>"AP费用申请","table"=>"ap_apply"],
                        12=>["name"=>"AP费用取消","table"=>"ap_apply"],
                        13=>["name"=>"AP费用异动","table"=>"ap_apply"],
                        14=>["name"=>"AP费用反馈","table"=>"ap_fankui"],
                        15=>["name"=>"AP费用核销","table"=>"ap_hexiao_apply"],
                        16=>["name"=>"AP补发票申请","table"=>"apti_fp_apply"],
                        17=>["name"=>"TI费用反馈","table"=>"all_ti_feeback_list"],
                        18=>["name"=>"TI费用核销","table"=>"cg_ti_hexiao"],
                        19=>["name"=>"TI补发票申请","table"=>"apti_fp_apply"],
                        20=>["name"=>"渠道优惠政策申请","table"=>"cg_channel_preferential_policy"],
                        21=>["name"=>"TI费用异动申请","table"=>"ti_apply"],
                        22=>["name"=>"TI费用取消申请","table"=>"ti_apply"],
                        23=>["name"=>"订单申请","table"=>"cg_order"],
                        25=>["name"=>"网点目标申请","table"=>"target_net_apply"],
                        26=>["name"=>"网点目标异动","table"=>"target_net_apply"],
                        27=>["name"=>"请假申请","table"=>"yd_qjsq"]
                    ];
    //列表页按钮提交工作流
    public function gowork($fid,$aid){
       // db("test_abs")->insert(["txt"=>$fid]);
        $flowId = db('cg_workflowset')->where(['id'=>$fid])->value('flow_id');
        $data = [];
        $data['accessToken'] = session('accessToken');
        $data['flowId'] = $flowId;
        $data = $this->curl_post(config('workflow_url').'/horizon/workflow/rest/flow/support/open.wf',$data);
         //db("test_abs")->insert(["txt"=>$data->flowMsg]);
        if($data->flowMsg=='提交成功'){
            $insert = [];
            
            if($fid==14){
                $row = db($this->typearr[$fid]["table"])->where('id',$aid)->field('no')->find();
                $insert['rwmcjbh'] ='AP费用反馈'.'['.$row['no'].']';
            }else if($fid==15){
                $row = db($this->typearr[$fid]["table"])->where('id',$aid)->field('no,apply_id')->find();
                $title=db("ap_apply")->where(["id"=>$row["apply_id"]])->value("title");
                $insert['rwmcjbh'] = $title."核销申请".'['.$row['no'].']';
            }else if($fid==16){
                $row = db($this->typearr[$fid]["table"])->where('id',$aid)->field('no,gid')->find();
                $title=db("gys")->where(["id"=>$row['gid']])->value("gys_name");
                $insert['rwmcjbh'] = "AP补发票申请".$title.'['.$row['no'].']';
            }else if($fid==17){
                $row = db($this->typearr[$fid]["table"])->where('id',$aid)->field('no,apply_id')->find();
                $title=db("cg_ti")->where(["id"=>$row['apply_id']])->value("title");
                $insert['rwmcjbh'] = $title.'['.$row['no'].']';
            }else if($fid==18){
                $row = db($this->typearr[$fid]["table"])->where('id',$aid)->field('sn as no,jxs_id')->find();
                $title=db("cg_jxs")->where(["id"=>$row['jxs_id']])->value("title");
                $insert['rwmcjbh'] = $title.'核销申请['.$row['no'].']';
            }else if($fid==19){
                $row = db($this->typearr[$fid]["table"])->where('id',$aid)->field('no,jid')->find();
                $title=db("cg_jxs")->where(["id"=>$row['jid']])->value("title");
                $insert['rwmcjbh'] = "TI补发票申请".$title.'['.$row['no'].']';
            }else if($fid==20){
                $row = db($this->typearr[$fid]["table"])->where('id',$aid)->field('sn,title')->find();
                $insert['rwmcjbh'] = "渠道优惠政策申请".$row["title"].'['.$row['sn'].']';
               // db("test_abs")->insert(["txt"=>$insert['rwmcjbh']]);
            }else if($fid==23){
                $row = db($this->typearr[$fid]["table"])->where('id',$aid)->field('sn,khmc as title')->find();
                $insert['rwmcjbh'] = "订单申请".$row["title"].'['.$row['sn'].']';
            }
            else if($fid==27){
                $row = db($this->typearr[$fid]["table"])->where('id',$aid)->field('sn,title')->find();
                $insert['rwmcjbh'] = "请假申请".$row["title"].'['.$row['sn'].']';
            }else{
                $row = db($this->typearr[$fid]["table"])->where('id',$aid)->field('title,no')->find();
                $insert['rwmcjbh'] = $row['title'].'['.$row['no'].']';
            }
            
            $insert['yw_id'] = $aid;
            $insert['ywlx'] = $fid;
            $insert['status'] = 1;
            $insert['department_id'] = db('department_employee')->where(['index'=>1,'employee_id'=>session('employee_id')])->value('department_id');
            $insert['sqr_id'] = session('employee_id');
            $insert['workId'] = $data->workId;
            $insert['trackId'] = $data->flowNode->trackId;
            db('cg_workflow')->insert($insert);
        }else{
            return json_encode(['state'=>2,'msg'=>$data->flowMsg]);die;
        }
        if($fid==18||$fid==20||$fid==23){
            $bool = db($this->typearr[$fid]["table"])->where('id',$aid)->update(['status'=>3]);
        }else
            $bool = db($this->typearr[$fid]["table"])->where('id',$aid)->update(['state'=>3]);
        if($fid==9){
            //异动则锁定原单
            $yuanid= db($this->typearr[$fid]["table"])->where(['id'=>$aid])->value("yuanid");
            db($this->typearr[$fid]["table"])->where(['id'=>$yuanid])->update(["islock"=>1]);
        }
        if($bool){
            return json_encode(['state'=>1,'msg'=>'提交成功']);
        }else{
            return json_encode(['state'=>2,'msg'=>'系统繁忙请稍后再试']);
        }
    }
    //直接提交工作流
    public function work($fid,$aid){
        $flowId = db('cg_workflowset')->where(['id'=>$fid])->value('flow_id');
        $data = [];
        $data['accessToken'] = session('accessToken');
        $data['flowId'] = $flowId;
        $data = $this->curl_post(config('workflow_url').'/horizon/workflow/rest/flow/support/open.wf',$data);
        if($data->flowMsg=='提交成功'){
            $insert = [];
            $row = db($this->typearr[$fid]["table"])->where('id',$aid)->field('title,no')->find();
            $insert['rwmcjbh'] = $row['title'].'['.$row['no'].']';
            $insert['yw_id'] = $aid;
            $insert['ywlx'] = $fid;
            $insert['status'] = 1;
            $insert['department_id'] = db('department_employee')->where(['index'=>1,'employee_id'=>session('employee_id')])->value('department_id');
            $insert['sqr_id'] = session('employee_id');
            $insert['workId'] = $data->workId;
            $insert['trackId'] = $data->flowNode->trackId;
            db('cg_workflow')->insert($insert);
            return json_encode(['state'=>1,'msg'=>""]);die;
        }else{
            return json_encode(['state'=>2,'msg'=>$data->flowMsg]);die;
        }
    }
    //获取审批信息
    public function getlog($fid,$aid){
        $result=[];
        $getwork=db('cg_workflow')->where(["ywlx"=>$fid,"yw_id"=>$aid])->value("workid");
        if($getwork){
            $result=Db::connect('db_config1')->name('tw_hz_log') 
            ->where(['workid'=>$getwork,"action"=>"submit"])
            ->select();
        }

        //print_r($result);
        return $result;
    }
    //获取按钮
    public function getbton($fid,$aid){


        $flow = db('cg_workflow')->where(["ywlx"=>$fid,"yw_id"=>$aid])->field("trackId,workId")->find();
        $workId=$flow["workId"];$trackId=$flow["trackId"];$nodeId="End1";

         //打开工作流
        $post_data = [];
        $post_data['accessToken'] = session('accessToken');
        $post_data['workId'] = $workId;
        $post_data['trackId'] = $trackId;
        $dataopen = $this->curl_post(config('workflow_url').'/horizon/workflow/rest/flow/support/open.wf',$post_data);


        $actionData = [
                    'operate'=>'submit',
                    'operateClazz'=>'com.horizon.wf.action.ActionSubmit',
                    'operateText'=>'提 交',
                    'operateFlag'=>1,
                ];
                $param = [
                    'accessToken'=>session("accessToken"),
                    'workId'=>$workId,
                    'trackId'=>$trackId,
                    
                    'actionData'=>$actionData,
                ];
        $data = $this->curl_post(config('workflow_url').'/horizon/workflow/rest/flow/support/action.wf',$param);
        $data = object_array($data);
       // print_r($data["nextNodes"]);
        return $data;
    }  
    //自动提交
    public function goworker($fid,$aid,$nodeKey,$selectId,$selectName){
        $flow = db('cg_workflow')->where(["ywlx"=>$fid,"yw_id"=>$aid])->field("trackId,workId")->find();
        $workId=$flow["workId"];$trackId=$flow["trackId"];
       
         $actionData = [
                    'operate'=>'submit',
                    'operateClazz'=>'com.horizon.wf.action.ActionSubmit',
                    'operateText'=>'提 交',
                    'operateFlag'=>0,
                    'nextNodes'=>[
                        [
                            'nodeKey'=>$nodeKey,
                            'selectId'=>$selectId,
                            'selectName'=>$selectName,
                            'isSelect'=>"true",
                            
                        ]
                    ],
                    "subFlows"=>null
                ];
                $param = [
                    'accessToken'=>session("accessToken"),
                    'workId'=>$workId,
                    'trackId'=>$trackId,
                    'actionData'=>$actionData,
                    "comment"=>"",
                    "dataId"=>"",
                ];
        $data = $this->curl_post(config('workflow_url').'/horizon/workflow/rest/flow/support/action.wf',$param);

        $data = object_array($data);
        return $data;
    }
        
    //测试接口
    public function back($workId){
        Db::table("test_abs")->insert(["txt"=>$workId]);
    }
     //预算申请
    public function yusuanshenqing($myId){
        $workfloawde=Db::table("cg_workflow")->where(["id"=>$myId])->find();
        $aid=$workfloawde["yw_id"];
        $this->assign("aid",$aid);
        $apply=Db::table("target_apply")->where("id=$aid")->find();
        $state=$apply["state"];
        $type_id=$apply["type_id"];
        $typearr=[
            1=>["id"=>1,"name"=>"集团目标格式","controller"=>"Jituanmubiao"],
            2=>["id"=>2,"name"=>"TI费用预算格式","controller"=>"Tiyusuan"],
            3=>["id"=>3,"name"=>"AP费用预算格式","controller"=>"Apyusuan"],
            4=>["id"=>4,"name"=>"日常费用预算格式","controller"=>"Rcyusuan"],
            5=>["id"=>5,"name"=>"经销商销售目标格式","controller"=>"Jingxiaomubiao"],
            6=>["id"=>6,"name"=>"个人发运目标格式","controller"=>"Mymubiao"],
             7=>["id"=>6,"name"=>"个人回款目标格式","controller"=>"Mymubiao"],
              8=>["id"=>6,"name"=>"个人零售目标格式","controller"=>"Mymubiao"]
        ];
        if($state==2){
            if($type_id<=5)
                $this->redirect($typearr[$type_id]["controller"].'/zuofei', ['tid' => $aid,"from"=>2]);
            else if($type_id==6)
                $this->redirect($typearr[$type_id]["controller"].'/fayunzuofei', ['tid' => $aid,"from"=>2]);
            else if($type_id==7)
                $this->redirect($typearr[$type_id]["controller"].'/huikuanzuofei', ['tid' => $aid,"from"=>2]);
            else if($type_id==8)
                $this->redirect($typearr[$type_id]["controller"].'/lingshouzuofei', ['tid' => $aid,"from"=>2]);
        }else{
            if($type_id<=5)
                $controller=url($typearr[$type_id]["controller"]."/detail");
            else if($type_id==6)
                $controller=url($typearr[$type_id]["controller"]."/fayundetail");
            else if($type_id==7)
                $controller=url($typearr[$type_id]["controller"]."/huikuandetail");
            else if($type_id==8)
                $controller=url($typearr[$type_id]["controller"]."/lingshoudetail");
            //echo $controller;exit;
            $this->assign("controller",$controller);
            return $this->fetch();
        }
    }
    //预算提交
    public function ystijiao($aid){
        $tmp=Db::table("target_apply")->where(["id"=>$aid])->find();
        $state=$tmp["state2"];
        if($state==1){
            $back=$this->gowork(8,$aid);
        }else{
            $back=$this->gowork(9,$aid);
        }
        
        return $back;
    }
    //预算作废
    public function yusuanzuofei(){
        $request = Request::instance();$data = $request->post();
        $aid=$data["tid"];
        $tmp=Db::table("target_apply")->where(["id"=>$aid])->find();
        $state=$tmp["state2"];
        if($state==1){
            $fid=8;
            //申请作废
            $res=$this->goworker($fid,$aid,$data["nodeKey"],$data["selectId"],$data["selectName"]);
            if($res["operateStatus"]=="success"){
                $up["id"]=$aid;
                $up["state"]=8;
                try{
                  Db::table("target_apply")->update($up);
                }catch (\Exception $e){
                
                  return json_encode(["msg"=>"系统繁忙请稍后再试","state"=>2]);
                }
                 return json_encode(["msg"=>"","state"=>1]);
            }else{
                return json_encode(["msg"=>"系统繁忙请稍后再试","state"=>2]);
            }
        }else{
            $fid=9;
            $res=$this->goworker($fid,$aid,$data["nodeKey"],$data["selectId"],$data["selectName"]);
            if($res["operateStatus"]=="success"){
                
                $up["id"]=$aid;
                $up["state"]=8;
                try{
                  Db::table("target_apply")->update($up);
                  Db::table("target_apply")->where(["id"=>$tmp["yuanid"]])->update(["tag"=>0,"nextid"=>null,"islock"=>0]);
                }catch (\Exception $e){
                
                  return json_encode(["msg"=>"系统繁忙请稍后再试","state"=>2]);
                }
                 return json_encode(["msg"=>"","state"=>1]);
            }
        }
    }

}